package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.IndicatorSystemData;
import com.ruoyi.system.mapper.IndicatorSystemMapper;
import com.ruoyi.system.service.IIndicatorSystemDataService;
import com.ruoyi.system.util.JudgeResultObject;
import com.ruoyi.system.util.SubjectiveWeightCalculateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IndicatorJudgementMapper;
import com.ruoyi.system.domain.IndicatorJudgement;
import com.ruoyi.system.service.IIndicatorJudgementService;

import static com.ruoyi.system.util.JudgeResultObject.parseFromJson;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-07
 */
@Service
public class IndicatorJudgementServiceImpl implements IIndicatorJudgementService
{
    @Autowired
    private IndicatorJudgementMapper indicatorJudgementMapper;

    @Autowired
    private IndicatorSystemMapper indicatorSystemMapper;
    @Autowired
    private SubjectiveWeightCalculateUtil subjectiveWeightCalculateUtil;
    @Autowired
    private IIndicatorSystemDataService indicatorSystemDataService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public IndicatorJudgement selectIndicatorJudgementById(Long id)
    {
        return indicatorJudgementMapper.selectIndicatorJudgementById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param indicatorJudgement 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<IndicatorJudgement> selectIndicatorJudgementList(IndicatorJudgement indicatorJudgement)
    {
        List<IndicatorJudgement> indicatorJudgementList = indicatorJudgementMapper.selectIndicatorJudgementList(indicatorJudgement);
        for(IndicatorJudgement ij:indicatorJudgementList){
            ij.setSystemName(indicatorSystemMapper.selectIndicatorSystemById(ij.getIndicatorSystemId()).getSystemName());
        }
        return indicatorJudgementList;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param indicatorJudgement 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIndicatorJudgement(IndicatorJudgement indicatorJudgement)
    {
        indicatorJudgement.setCreateTime(DateUtils.getNowDate());
        if(indicatorJudgement.getJudgeMatrix() != null)
            indicatorJudgement.setJudgeMatrix(parseFromJson(indicatorJudgement.getJudgeMatrix()).toJsonString());
        return indicatorJudgementMapper.insertIndicatorJudgement(indicatorJudgement);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param indicatorJudgement 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIndicatorJudgement(IndicatorJudgement indicatorJudgement)
    {
        return indicatorJudgementMapper.updateIndicatorJudgement(indicatorJudgement);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorJudgementByIds(Long[] ids)
    {
        return indicatorJudgementMapper.deleteIndicatorJudgementByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorJudgementById(Long id)
    {
        return indicatorJudgementMapper.deleteIndicatorJudgementById(id);
    }

    @Override
    public int updateIndicatorJudgementStatus(Long sysId, HashMap<Long, Double> resMap) {
        int count = 0;
        for (Map.Entry<Long, Double> entry : resMap.entrySet()) {
            Double averageValue = entry.getValue() ;
            IndicatorSystemData indicatorSystemData = new IndicatorSystemData();
            indicatorSystemData.setIndicatorId(entry.getKey());
            indicatorSystemData.setSystemId(sysId);
            indicatorSystemData.setSubjectiveWeight(Float.parseFloat(String.valueOf(averageValue)));
            indicatorSystemDataService.updateIndicatorSystemData(indicatorSystemData);
            count++;
        }
        return count;
    }

    @Override
    public JudgeResultObject calculateSubjectiveWeight(Long[] ids) {
        // 获取第一个 IndicatorJudgement 的 indicatorsystemid 作为基准
        IndicatorJudgement firstIndicatorJudgement = indicatorJudgementMapper.selectIndicatorJudgementById(ids[0]);
        if (firstIndicatorJudgement == null) {
            throw new IllegalStateException("评价号为" + ids[0] + "的评价记录不存在！");
        }
        if (firstIndicatorJudgement.getStatus() == 3) {
            throw new IllegalStateException("评价号为" + ids[0] + "的评价还没有专家打分！");
        }
        Long baseIndicatorSystemId = firstIndicatorJudgement.getIndicatorSystemId();
        // 创建用于存储结果的 Map
        Map<Long, Double> resMap = new HashMap<>();

        System.out.println("正在计算评价号为" + ids[0] + "的评价记录的主观权重...");

        resMap = subjectiveWeightCalculateUtil.calculateSubjectiveWeight(firstIndicatorJudgement);
        firstIndicatorJudgement.setStatus(1L);
        indicatorJudgementMapper.updateIndicatorJudgement(firstIndicatorJudgement);
        // 遍历所有的 ids
        for (int i = 1; i < ids.length; i++) {
            System.out.println("正在计算评价号为" + ids[i] + "的评价记录的主观权重...");
            IndicatorJudgement indicatorJudgement = indicatorJudgementMapper.selectIndicatorJudgementById(ids[i]);
            if (indicatorJudgement == null) {
                throw new IllegalStateException("评价号为" + ids[i] + "的评价记录不存在！");
            }
            if (indicatorJudgement.getStatus() == 3) {
                throw new IllegalStateException("评价号为" + ids[i] + "的评价还没有专家打分！");
            }
            if (!baseIndicatorSystemId.equals(indicatorJudgement.getIndicatorSystemId())) {
                throw new IllegalStateException("评价号为" + ids[i] + "的评价记录与其他记录的指标系统ID不一致！");
            }

            // 计算主观权重
            HashMap<Long, Double> map = subjectiveWeightCalculateUtil.calculateSubjectiveWeight(indicatorJudgement);
            for (Map.Entry<Long, Double> entry : map.entrySet()) {
                if (resMap.containsKey(entry.getKey())) {
                    resMap.put(entry.getKey(), resMap.get(entry.getKey()) + entry.getValue());
                } else {
                    resMap.put(entry.getKey(), entry.getValue());
                }
            }
            indicatorJudgement.setStatus(1L);
            indicatorJudgementMapper.updateIndicatorJudgement(indicatorJudgement);
        }
         //计算平均值并更新数据库
        for (Map.Entry<Long, Double> entry : resMap.entrySet()) {
            resMap.put(entry.getKey(), entry.getValue() / ids.length);
//            IndicatorSystemData indicatorSystemData = new IndicatorSystemData();
//            indicatorSystemData.setIndicatorId(entry.getKey());
//            indicatorSystemData.setSystemId(baseIndicatorSystemId);
//            indicatorSystemData.setSubjectiveWeight(Float.parseFloat(String.valueOf(averageValue)));
//            indicatorSystemDataService.updateIndicatorSystemData(indicatorSystemData);
        }
        JudgeResultObject resultObject = JudgeResultObject.parseFromJson(firstIndicatorJudgement.getJudgeMatrix());
        resultObject.setSubjectWeight(resMap);
        resultObject.setResultMap(resMap);
        return resultObject;
    }

}
